package com.example.user;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    public Handler handler;
    @BindView(R.id.et_main_content)
    EditText et_main_content;
    @BindView(R.id.tv_receive_content)
    TextView tv_receive_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x1234){
                    tv_receive_content.setText(tv_receive_content.getText().toString()+msg.getData().getString("receive_content"));
                }
            }
        };
        try {
            new Thread(new ClientThread(handler)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    @OnClick(R.id.btn_main_send)
    void send(){
        Message msg=new Message();
        msg.what=0x1234;
        Bundle bundle=new Bundle();
        bundle.putString("send_content",et_main_content.getText().toString()+"\n");
        msg.setData(bundle);
        ClientThread.SendHandler.sendMessage(msg);
    }
    @OnClick(R.id.btn_main_connect)
    void connect(){
        try {
            new Thread(new ClientThread(handler)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ClientThread implements  Runnable{
    public static Handler SendHandler=null;
    Socket sk=null;
    Handler RecHandler=null;
    BufferedReader br=null;
    OutputStream os=null;
    public ClientThread(Handler handler) throws IOException {
        this.RecHandler=handler;
    }

    @Override
    public void run() {
        try {
            sk=new Socket("127.0.0.1",30000);
            br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
            os=sk.getOutputStream();
            new Thread(){
                @Override
                public void run() {
                    String content=null;
                    try {
                        while ((content = br.readLine()) != null) {
                            Message msg = new Message();
                            msg.what = 0x1234;
                            Bundle bundle = new Bundle();
                            bundle.putString("receive_content", content);
                            msg.setData(bundle);

                            RecHandler.sendMessage(msg);

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Looper.prepare();
        SendHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x1234){
                    try {
                        os.write(msg.getData().getString("send_content").getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Looper.loop();

    }
}